﻿using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;

namespace hCommand {
    // 使用方法
    // HttpClient client = new HttpClient(new HttpHandler("{商户号}", "{商户证书序列号}"));
    // ...
    // var response = client.GetAsync("https://api.mch.weixin.qq.com/v3/certificates");
    public class HttpHandler {

        public static string merchantId= "1639883614";
        public static string serialNo= "7195D418BBF05A3028611239C57E3BCA437D9B3D";

        public static string appId = "wx5e4abb94037bfaac";


        public static String SendAsync(String uri,String body) {

            var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
            string nonce = Path.GetRandomFileName();

            string message = $"POST\n{uri}\n{timestamp}\n{nonce}\n{body}\n";
            string signature = Sign(message);
            String auth= $"mchid=\"{merchantId}\",nonce_str=\"{nonce}\",timestamp=\"{timestamp}\",serial_no=\"{serialNo}\",signature=\"{signature}\"";
            string value = $"WECHATPAY2-SHA256-RSA2048 {auth}";
           
        
            return value;
        }



       public  static string Sign(string message) {
            // NOTE： 私钥不包括私钥文件起始的-----BEGIN PRIVATE KEY-----
            //        亦不包括结尾的-----END PRIVATE KEY-----
            string privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDTMT9KOzvGIgiuiIDErZ48h6FuqUrPi3oq7tDeGwGWu1ptZEHiQpuzaSwPc2I6+kPYB2n2+dNsZJlG"+
                "g7Ca9ZaCHqdL5b+znt06T2l6+AjrxupNuCj+W1vhXdacMWM206CDUPTz7xc1ITnEKhsrskk9tg2vuyu+X+wrZXbOvgiGR9Vu5Ym9WSkWyllaWfY0nJF73fZZw6pMD08C"
                +"UoSE9Qo5+GsHeLLz386AGrs1C2osHZwJPtFs0Hkea3kvso1I4I8tVl0PNCtBT+0gXfd2HQd0hHyIA4LE7lhNE1kyCP2d0bLErGGd/aaALG1BMgrtr88jptUNY6oFdKIA"
                +"BIjHKNAvAgMBAAECggEAHGZZ+XR/tEQtYLMQheJHDiK7DbOMUN6TviRBSCzfiNKG9YDu7ys/3t98wlMk2YOADhPpJwIvcTsR9Y6N+DigVb0jiCxMHC5313iIWbXzFSm5"
                +"plvmf1XvxB2nV3e4vM22nxIK7qavO0DNkNiMsNKX0Mmji4ci+cXSb3+Ee4bIhK3zqAyohBQxdylBBLcmo0O7tHBwzFVJBIh1PQbyiWZKAx+5D5IBpSIhUGK+lbqVzRuf"
                +"Zs3OsEf/Y0Ed+1ZflEhTiobk4VV/nyXXV5EVL4QZeNr7NH9Vc6L4w4wTGrYwxWksWo/Um9RqNlnvPdxbEHB51Tn2t4Ofyt9s7W6+PewlAQKBgQDy51EgvdmSh7vdZqdt"
                +"nY8GkRIID93XuSgireN78PtkscunNLbweCBAHjXRECiWlbvzFNJjlo8pHKJ2+AeZhmoZ/96mUw6VL9NNI6TZ7JU42dpDbgaDHBS1W+KTCNIpuRC7USLb2M/hqMQAh5gY"
                +"K8oveeh6Z2S6iZ2bhrZs6HWHgQKBgQDelDxKiMmlCyD0GGi9yz3K0VipVdWENSa8oE07+5+damEao6OaA/X6qImHzYilaPeXR/0S4pShvZ+5f0LCasUCNbvRzRIgFerv"
                +"XRAfDzN7JowC6cDBLZ3BxqtAI8YMosmSV8p/egfrC91Ef92abavPbTakvGk2lC0DXq17nzCvrwKBgQDOaw7BYTSc1iYq/sp3V3as4OlO2pTudtWJqvXvZJluN71WKXDx"
                +"J1RYyJN5R18GoNYou2AS62ZbTI8vxJVIvTY7Q/pko5533z2Uhx7XZrpkppVZyg1Uq69Zc203AD8A39k9JbUW2MfH/5AY1yHEIKpf8BxEsMqjzsCV40PxhxYhgQKBgC+7"
                +"7L4zXcNmgpe20Ge7ubHQeco/HT5wQ7F6U3YEvbN9x/t1krWKGeu6z1vviVmoDElGaBDvxN53E1Ut5LxbN4sEyRXye+ujp7HT7Iwsr2a+2pIZXXxQcx/Gop1rzqKRGtem"
                +"ukfifOY8siRpo7XlxU4D5w8nVQ0edBTf7iav+joDAoGBAIsssB5sRIHbS9cUyd9udlDNz+o5cYNpCp0v2sKmO4NRzCVNygEzn9xsvtjSYZ6p8RXEa4m/QqknawzG+qnP"
                +"1tbVOM6xJB2d7o2bb/rd6iI8M3seI01h6UykF1/IbPeBs/aYrxEdwTS7ySdSDM3qTRn7vWgmHfA5y0Ua18fxDO+7";
            byte[] keyData = Convert.FromBase64String(privateKey);

            var rsa = RSA.Create();
            //适用该方法的版本https://learn.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.asymmetricalgorithm.importpkcs8privatekey?view=net-7.0
            rsa.ImportPkcs8PrivateKey(keyData, out _);
            rsa.ImportPkcs8PrivateKey(keyData, out _);
            byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
            return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
        }
    }
}
